﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using app.Models;
using AmuTools;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace app.Api
{
    /*
        公用接口
    */
    public class ApiMemberMessage : ApiBase
    {
        public ApiMemberMessage(Controller ctx) : base(ctx) { }

        #region 自定义请求参数
        // 作者
        int[] req_idList { get { return GetObjectParam<int[]>(new ReqObjectParamOption<int[]> { Name = "idList" }); } }
        #endregion

        public void Do(string action)
        {
            switch (action)
            {
                case "exist":
                    {
                        List<MemberMessage> list = LandingServer.GetNewMessage(UserCode);
                        ResponseJson(new ListResult<MemberMessage> { list = list });
                        break;
                    }
                case "unreadList":
                    {
                        Ob ob = new Ob();
                        ob.Desc("create_time");
                        ResponseJson(GetPageResult<MemberMessage>(string.Format("m_type = 0 and status = 0 and user_id = {0}", UserCode), ob.Render()));
                        break;
                    }
                case "readList":
                    {
                        Ob ob = new Ob();
                        ob.Desc("create_time");
                        ResponseJson(GetPageResult<MemberMessage>(string.Format("m_type = 0 and status = 1 and user_id = {0}", UserCode), ob.Render()));
                        break;
                    }
                case "recycleList":
                    {
                        Ob ob = new Ob();
                        ob.Desc("create_time");
                        ResponseJson(GetPageResult<MemberMessage>(string.Format("m_type = 0 and status = 2 and user_id = {0}", UserCode), ob.Render()));
                        break;
                    }
                case "checkMessage":
                    {
                        MemberMessage memberMessage = G.DB.GetById<MemberMessage>(req_id);
                        memberMessage.status = 1;
                        G.DB.Update<MemberMessage>(memberMessage);
                        ResponseJson(new Result());
                        break;
                    }
                case "toRecycle":
                    {
                        MemberMessage memberMessage = G.DB.GetById<MemberMessage>(req_id);
                        if (memberMessage.status == 2) ThrowStatus(STATUS.FAILED_UPDATE, "已经在回收站中");
                        memberMessage.status = 2;
                        G.DB.Update<MemberMessage>(memberMessage);
                        ResponseJson(new Result());
                        break;
                    }
                case "getBack":
                    {
                        MemberMessage memberMessage = G.DB.GetById<MemberMessage>(req_id);
                        if (memberMessage.status == 1) ThrowStatus(STATUS.FAILED_UPDATE, "已经还原");
                        memberMessage.status = 1;
                        G.DB.Update<MemberMessage>(memberMessage);
                        ResponseJson(new Result());
                        break;
                    }
                case "delete":
                    {
                        G.DB.Delete<MemberMessage>(req_id);
                        ResponseJson(new Result());
                        break;
                    }
                case "toRecycleList":
                    {
                        for (int i = 0; i < req_idList.Length; i++)
                        {
                            MemberMessage memberMessage = G.DB.GetById<MemberMessage>(req_idList[i]);
                            memberMessage.status = 2;
                            G.DB.Update<MemberMessage>(memberMessage);
                        }
                        ResponseJson(new Result());
                        break;
                    }
                case "checkMessageList":
                    {
                        for (int i = 0; i < req_idList.Length; i++)
                        {
                            MemberMessage memberMessage = G.DB.GetById<MemberMessage>(req_idList[i]);
                            memberMessage.status = 1;
                            G.DB.Update<MemberMessage>(memberMessage);
                        }
                        ResponseJson(new Result());
                        break;
                    }
                case "deleteList":
                    {
                        for (int i = 0; i < req_idList.Length; i++)
                        {
                            G.DB.Delete<MemberMessage>(req_idList[i]);
                        }
                        ResponseJson(new Result());
                        break;
                    }
                case "send":
                    {
                        long time = long.Parse(GetTimeStampParam(new ReqTimeStampParamOption { Name = "time", Level = REQPARAMLEVEL.FREE }));
                        int id = req_id;
                        string message_title = GetStringParam(new ReqStringParamOption { Name = "message_title" });
                        string mediums = GetStringParam(new ReqStringParamOption { Name = "medium" });
                        Member m = G.DB.GetById<Member>(id);
                        string content = req_content;
                        int user_code = int.Parse(UserCode);
                        TimerHelper.Add(state => {
                            if(mediums.Contains("system"))
                                SendMessage(id, message_title, content, user_code);
                            if(mediums.Contains("phone"))
                                SendSMS(id, m.mobile_phone,content,m.username, user_code);
                            if (mediums.Contains("email"))
                                SendEmail(message_title, content, m.h_email);
                        }, time);
                        ResponseJson(new Result());
                        break;
                    }
                case "send_batch":
                    {
                        long time = long.Parse(GetTimeStampParam(new ReqTimeStampParamOption { Name = "time", Level = REQPARAMLEVEL.FREE }));
                        string message_title = GetStringParam(new ReqStringParamOption { Name = "message_title" });
                        string mediums = GetStringParam(new ReqStringParamOption { Name = "medium" });
                        string content = req_content;
                        int user_code = int.Parse(UserCode);
                        string condition = Member.GetSearchSql(
                            GetIntParam(new ReqIntParamOption { Name = "id", Level = REQPARAMLEVEL.FREE }),
                            GetIntParam(new ReqIntParamOption { Name = "role_type", Level = REQPARAMLEVEL.FREE }),
                            GetStringParam(new ReqStringParamOption { Name = "username", Level = REQPARAMLEVEL.FREE }),
                            GetStringParam(new ReqStringParamOption { Name = "area", Level = REQPARAMLEVEL.FREE }),
                            GetStringParam(new ReqStringParamOption { Name = "member_type", Level = REQPARAMLEVEL.FREE }),
                            GetStringParam(new ReqStringParamOption { Name = "title", Level = REQPARAMLEVEL.FREE }),
                            GetTimeStampParam(new ReqTimeStampParamOption { Name = "begin_time", Level = REQPARAMLEVEL.FREE, DefaultValue = "" }),
                            GetTimeStampParam(new ReqTimeStampParamOption { Name = "end_time", Level = REQPARAMLEVEL.FREE, DefaultValue = "" }),
                            GetIntParam(new ReqIntParamOption { Name = "status", Level = REQPARAMLEVEL.FREE, DefaultValue = -1 })
                        );
                        TimerHelper.Add(state => {
                            Ob ob = new Ob();
                            ob.Desc("create_time"); // 降序
                            DataTable dt = G.DB.Get<Member>(condition, ob.Render(), 0).FirstTable;
                            foreach(DataRow dr in dt.Rows)
                            {
                                //SendMessage((int)dr["id"], message_title, content, user_code);
                                if (mediums.Contains("system"))
                                    SendMessage((int)dr["id"], message_title, content, user_code);
                                if (mediums.Contains("phone"))
                                    SendSMS((int)dr["id"], (string)dr["mobile_phone"], content, (string)dr["name"], user_code);
                                if (mediums.Contains("email"))
                                    SendEmail(message_title, content, (string)dr["email"]);
                            }
                        }, time);
                        ResponseJson(new Result());
                        break;
                    }
                default: break;
            }
        }
    }
}